package com.quick.controller.listener;

import com.quick.common.annotation.DataScopeByGetInfo;
import com.quick.common.core.event.DataScopeEvent;
import com.quick.common.exception.ServiceException;
import com.quick.system.service.SysDeptService;
import com.quick.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 监听
 *
 * @Author: Administrator
 * @Date: 2023/8/11
 */
@Component
public class DataScopeListener {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;

    @EventListener
    public void dataScopeEvent(DataScopeEvent dataScopeEvent) {
        Object target = dataScopeEvent.getTarget();
        Long[] ids = new Long[0];
        if (target.getClass().isArray()) {
            Object[] obj = (Object[]) target;
            ids = new Long[obj.length];
            for (int i = 0; i < obj.length; i++) {
                ids[i] = Long.valueOf(obj[i].toString());
            }
        }
        if (dataScopeEvent.getService().equals(DataScopeByGetInfo.DEPT)) {
            if (target.getClass().isArray()) {
                sysDeptService.checkDeptDataScope(ids);
            } else {
                sysDeptService.checkDeptDataScope(Long.valueOf(target.toString()));
            }
        } else if (dataScopeEvent.getService().equals(DataScopeByGetInfo.USER)) {
            if (target.getClass().isArray()) {
                sysUserService.checkUserDataScope(ids);
            } else {
                sysUserService.checkUserDataScope(Long.valueOf(target.toString()));
            }
        }
    }
}
